import mx.transitions.*;
import mx.transitions.easing.*;

class lib.resolver.TxtResolver
{
	private var _content:String;
	private var display_tf:TextField;
	private var tf_fmt:TextFormat;
	private var _path:MovieClip;

	private var blockArr:Array;
	private var blockStartScroll:Number;
	private var block_tf:TextField;
	private var highlightColor:Number=0x0000ff;
	private var currentBlockNum:Number;
	// private var block_rect:MovieClip;
	private var block_line:TextField;

	private var filling_rect:MovieClip;
	private var fillingSpeed:Number=300;	// ms
	private var fillingTween:Tween;
	private var threshold:Number=0.8;

	private var _status:Object;		// format, blockMin, blockMax, displayScroll, blockNum
	
	public function TxtResolver(path:MovieClip)
	{
		trace("*TxtResolver constructor..."+path);
		_path=path;
		tf_fmt=new TextFormat();
		_status=new Object();
	}

	public function get path():MovieClip
	{
		return _path;
	}

	public function set content(cont:String)
	{
		// for mode 0
		// _content=cont;
		// for mode 1
		_content="";
		var resArr=cont.split("\r\n");
		for (var i=0; i<resArr.length; i++)
		{
			if (resArr[i].charAt(0)==" ")
			{
				_content+="\r\n";
				// trace(_content.length);
			}
			_content+=resArr[i];
		}
		// append n-1 lines \r\n
		// var scrollNumPerPage=calcTotalPageScroll();
		_content+="\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n";
	}

	public function get content():String
	{
		return _content;
	}

	public function set speed(fs:Number)
	{
		fillingSpeed=fs;
		_status.blockSpeed=fillingSpeed;
	}

	public function get speed():Number
	{
		return fillingSpeed;
	}

	public function set blockColor(bc:Number)
	{
		highlightColor=bc;
		_status.blockColor=highlightColor;
	}

	public function get blockColor():Number
	{
		return highlightColor;
	}

	public function set status(statObj:Object)
	{
		_status=statObj;
		if (statObj.blockSpeed!=null)
		{
			fillingSpeed=statObj.blockSpeed;
		}
		if (statObj.blockColor!=null)
		{
			highlightColor=statObj.blockColor;
		}
		changeDisplayFormat(statObj.format);
		trace(statObj.blockMin);
		scanBlocks(statObj.blockMin, statObj.blockMax);
		initFillingBlock(_path._width, 20, 0, -20);
		// currentBlockNum=statObj.blockNum;
		/*disposePreviousBlock();
		generateCurrentBlock(statObj.displayScroll-display_tf.scroll);*/
	}

	public function get status():Object
	{
		return _status;
	}
	
	public function createDisplayTextField()
	{
		display_tf=_path.createTextField("displayContent", _path.getNextHighestDepth(), 0, 0, _path._width, _path._height);
		display_tf.selectable=false;
		//
		/*block_rect=_path.createEmptyMovieClip("block_rect", _path.getNextHighestDepth());
		block_rect.moveTo(0, 0);
		block_rect.beginFill(0x00ff00, 60);
		block_rect.lineTo(100, 0);
		block_rect.lineTo(100, 100);
		block_rect.lineTo(0, 100);
		block_rect.lineTo(0, 0);
		block_rect.endFill();
		block_rect._visible=false;
		*/
	}

	public function refreshDisplayContent()
	{
		// trace("refreshDisplayContent: "+_content);
		display_tf.wordWrap=true;
		// display_tf.selectable=false;
		display_tf.text=_content.substr(0, 200);
	}

	public function changeDisplayFormat(formatObj:Object)
	{
		_status.format=formatObj;
		for (var a in formatObj)
		{
			tf_fmt[a]=formatObj[a];
		}
		display_tf.setTextFormat(tf_fmt);
	}

	public function scanBlocks(blockMin:Number, blockMax:Number)
	{
		if (blockMin==null)
		{
			blockMin=100;
		}
		_status.blockMin=blockMin;
		_status.blockMax=blockMax;
		blockArr=new Array();
		// blockStartScroll=1;
		// currentBlockNum=0;
		var midContent=_content;
		var cacheString="";
		var index=midContent.indexOf("\r\n");
		while (index>-1)
		{
			cacheString+=midContent.substr(0, index+4);
			midContent=midContent.substr(index+4);
			index=midContent.indexOf("\r\n");

			if (cacheString.length>=blockMin)
			{
				blockArr.push(cacheString);
				// trace("cacheString: ["+cacheString.length+"] "+cacheString);
				cacheString="  ";
			}
		}
		// trace("midContent: "+cacheString+midContent);
		blockArr.push(cacheString+midContent);

		/*for (var i=0; i<blockArr.length; i++)
		{
			trace(i+": "+blockArr[i]);
		}*/
	}

	public function switchBlock(blockOffset:Number)
	{
		var scrollOffset:Number;
		if (currentBlockNum==null)
		{
			currentBlockNum=0;
			scrollOffset=0;
			// display_tf.scroll=blockStartScroll;
		}
		else
		{
			currentBlockNum+=blockOffset;
			if (currentBlockNum<0) return;
			else if (currentBlockNum>=blockArr.length) return;		

			// trace(block_line.length+": "+block_line.maxscroll);
			if (blockOffset>0)
			{
				scrollOffset=block_line.maxscroll-1;
			}
			else scrollOffset=blockStartScroll-display_tf.scroll;
			// trace("block_tf.maxscroll: "+block_tf.maxscroll);
			
			disposePreviousBlock();
		}
		trace("scrollOffset: "+scrollOffset);
		generateCurrentBlock(scrollOffset);
	}

	public function initFillingBlock(width:Number, height:Number, x:Number, y:Number)
	{
		filling_rect.removeMovieClip();
		filling_rect=_path.createEmptyMovieClip("filling_rect", _path.getNextHighestDepth());
		filling_rect._x=x?x:0;
		filling_rect._y=y?y:0;
		var bg=filling_rect.createEmptyMovieClip("bg", filling_rect.getNextHighestDepth());
		// bg.lineStyle(-1, 0x000000);
		bg.moveTo(0, 0);
		bg.beginFill(0x00ff00, 60);
		bg.lineTo(width, 0);
		bg.lineTo(width, height);
		bg.lineTo(0, height);
		bg.lineTo(0, 0);
		bg.endFill();
		var masker=bg.duplicateMovieClip("masker", filling_rect.getNextHighestDepth());
		bg.setMask(masker);
		var textLayer=filling_rect.createTextField("textLayer", filling_rect.getNextHighestDepth(), 0, 0, 100, 20);
		textLayer.textColor=0xff0000;
		// textLayer.text=blockArr[currentBlockNum].length;
	}
	
	public function startFillingBlock()
	{
		// trace("filling_rect.masker: "+filling_rect.masker);
		// trace("start: "+-filling_rect.masker._width);
		// trace("end: "+filling_rect.masker._x);
		// trace("duration: "+Math.round(blockArr[currentBlockNum].length*speed/1000));
		filling_rect._visible=true;
		var gap=threshold;
		var totalLength=blockArr[currentBlockNum].length;
		var textLayer=filling_rect.textLayer;
		fillingTween=new Tween(filling_rect.masker, "_width", None.easeNone, 0, filling_rect.bg._width, Math.round(blockArr[currentBlockNum].length*speed/1000), true);
		fillingTween.onMotionChanged=function(target)
		{
			// trace("onMotionChanged..."+target._time);
			// trace(target.obj._x+" "+target.obj._width);
			var progress=Math.round(target._time/target._duration*totalLength);
			textLayer.text=progress+" / "+totalLength;
			if (progress/totalLength>gap)
			{
				textLayer.textColor=0x000000;
			}
			else textLayer.textColor=0xff0000;
		}
	}

	public function stopFillingBlock()
	{
		fillingTween.stop();
		filling_rect._visible=false;
	}

	public function get blockLength():Number
	{
		return block_tf.length;
	}

	public function set blockNum(bNum:Number)
	{
		currentBlockNum=bNum;
	}

	public function get blockNum():Number
	{
		return currentBlockNum;
	}

	private function disposePreviousBlock()
	{
		block_tf.removeTextField();
		block_line.removeTextField();
		// block_rect._visible=false;
	}

	private function generateCurrentBlock(scrollOffset:Number)
	{
		blockStartScroll=display_tf.scroll;
		display_tf.scroll+=scrollOffset;
		_status.displayScroll=display_tf.scroll;
		_status.blockNum=currentBlockNum;
		block_tf=_path.createTextField("displayBlock", _path.getNextHighestDepth(), 0, 0, _path._width, _path._height);
		block_line=_path.createTextField("displayBlock", _path.getNextHighestDepth(), 0, 0, _path._width, 20);
		// block_tf._visible=false;
		// block_tf.autoSize=true;
		block_tf.wordWrap=true;
		block_tf.selectable=false;
		block_tf.text=blockArr[currentBlockNum];
		// trace("tf.length: "+block_tf.length);
		// trace("text.length: "+block_tf.text.length);
		block_tf.setTextFormat(tf_fmt);
		 block_tf.textColor=highlightColor;

		block_line.wordWrap=true;
		// block_line.selectable=false;
		// block_tf.textColor=0x0000ff;
		block_line.text=blockArr[currentBlockNum];
		block_line.setTextFormat(tf_fmt);
		block_line._visible=false;

		/*block_rect._height=block_tf.textHeight;
		block_rect._width=block_tf.textWidth;
		block_rect._visible=true;*/
		// initFillingBlock(_path._width, 20, 0, -20);
	}
}